package com.bycx.framework.shiro.realm;

import com.bycx.framework.shiro.service.IShiroRealmService;
import org.apache.log4j.Logger;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class ShiroRealm extends AuthorizingRealm {

	private static final Logger logger = Logger.getLogger(ShiroRealm.class);
	
	@Autowired
	private IShiroRealmService ShiroRealm;
	
	/**
	 * 认证回调函数 登录时调用
	 */
	@Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) {
		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
		return new SimpleAuthenticationInfo(token.getUsername() ,token.getCredentials(), getName());
    }


	/**
	 * 权限授权
	 */
	@Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		logger.debug("=========================================================================");
		logger.debug("AuthorizingRealm doGetAuthorizationInfo start");
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        String principal = (String) principals.fromRealm(getName()).iterator().next();
        
        //角色roles授权
  
        //许可perms授权
        
        
        logger.debug("登录用户：" + principal);
        logger.debug("AuthorizingRealm doGetAuthorizationInfo end");
		logger.debug("=========================================================================");
        return authorizationInfo;  
    }

}

